package kalender.backend;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class EventType {
	private int etype;
	private String et_desc;
	
	public int getEtype() { return etype; }
	public void setEtype(int e) { etype = e; }
	
	public String getEt_desc() { return et_desc; }
	public void setEt_desc(String e) { et_desc = e; }
	
	public String toString() { return "etype: " + etype + " et_desc: " + et_desc; }
	
	public static EventType find(int etype) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from eventtype where etype = ?");
			ps.setInt(1, etype);
			ResultSet rs = ps.executeQuery();
			SQLWarning warn1 = ps.getWarnings();
			DatabaseManager.printWarning(warn1);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			if (rs.next()) {
				EventType et = new EventType();
				et.etype = rs.getInt("etype");
				et.et_desc = rs.getString("et_desc");
				return et;
			} else {
				return null;
			}
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static EventType find(String desc) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from eventtype where et_desc = ?");
			ps.setString(1, desc);
			ResultSet rs = ps.executeQuery();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			
			if (rs.next()) {
				EventType et = new EventType();
				et.et_desc = rs.getString("et_desc");
				et.etype = rs.getInt("etype");
				return et;
			} else {
				return null;
			}
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static List<EventType> findAll() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select * from eventtype");
			ResultSet rs = ps.executeQuery();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			
			ArrayList<EventType> ets = new ArrayList<EventType>();
			while (rs.next()) {
				EventType et = new EventType();
				et.et_desc = rs.getString("et_desc");
				et.etype = rs.getInt("etype");
				ets.add(et);
			}
			return ets;
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static List<EventType> findByUser(Luser user) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("select distinct etype, et_desc from event natural join eventtype where uid = ?;");
			ps.setInt(1, user.getUid());
			ResultSet rs = ps.executeQuery();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			SQLWarning warn2 = rs.getWarnings();
			DatabaseManager.printWarning(warn2);
			
			ArrayList<EventType> ets = new ArrayList<EventType>();
			while (rs.next()) {
				EventType et = new EventType();
				et.et_desc = rs.getString("et_desc");
				et.etype = rs.getInt("etype");
				ets.add(et);
			}
			return ets;
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveNew() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into eventtype (et_desc) values(?)", Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, et_desc);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
			ResultSet keys = ps.getGeneratedKeys();
			keys.next();
			etype = keys.getInt(1);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveUpdate() throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("update eventtype set et_desc = ? where etype = ?");
			ps.setString(1, et_desc);
			ps.setInt(2, etype);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static void delete(int etype) throws BackendException {
		Connection conn = DatabaseManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("delete from eventtype where etype = ?");
			ps.setInt(1, etype);
			ps.executeUpdate();
			SQLWarning warning = ps.getWarnings();
			DatabaseManager.printWarning(warning);
		} catch (SQLException e) {
			DatabaseManager.printException(e);
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
}
